package Collection;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;

/**
 * @author wuhan
 * @date 2019/9/26 14:03
 */

class Student implements Comparable<Student>{
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return name.hashCode()+age*39;
    }

    @Override
    public String toString() {
        return name+":"+age;
    }

    private String name;
    private int age;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(Student student) {
        int num= Integer.compare(this.age, student.age);
        if (num==0)
            return this.name.compareTo(student.getName());
        return num;
    }

}

class MyCompare implements Comparator{

    @Override
    public int compare(Object o1, Object o2) {
        Student s1 = (Student) o1;
        Student s2 = (Student) o2;
        int flag =  s1.getName().compareTo(s2.getName());
        if (flag==0){
            return s1.getAge()-s2.getAge();
        }else {
        return flag;
        }
    }
}







public class TreeSetDemo {
    public static void main(String[] args) {
        TreeSet ts=new TreeSet(new MyCompare());    ts.add(new Student("ljh",20));
        ts.add(new Student("wuhan",19));
        ts.add(new Student("ljy",18));
        ts.add(new Student("lzy",19));
        ts.add(new Student("wuhan",29));


        for (Object t : ts) {
            Student stu = (Student) t;
            System.out.println(stu.getName() + "  " + stu.getAge());
        }

    }
}
